//
//  LRAppDelegate.m
//  Anyair
//
//  Created by karlcool on 14-5-11.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//  百度地图KEY:TgNmNyhGwvQqAzAl24dZe8WW

#import "LRAppDelegate.h"
#import "CoverViewController.h"
#import "BMapKit.h"

@implementation LRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (IOS_7) {
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CoverViewController *cover = [[CoverViewController alloc]initWithNibName:@"CoverViewController" bundle:nil];
    self.window.rootViewController = cover;

    //初始化百度地图
    BMKMapManager *mapManager = [[BMKMapManager alloc]init];
    
    BOOL ret = [mapManager start:@"TgNmNyhGwvQqAzAl24dZe8WW" generalDelegate:nil];
    if (!ret) {
        NSLog(@"百度地图启动失败");
    }
    
    //连接已经配对过的设备
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"connectedUUID"]!=nil) {
        [[BluetoothManager createManager] connectDevice];
    }

    //检查登录状态
    [LRUserManager createManager].isLogIn = [[NSUserDefaults standardUserDefaults]boolForKey:@"isLogIn"];
    if ([LRUserManager createManager].isLogIn) {
        [LRUserManager createManager].userPhone = [[NSUserDefaults standardUserDefaults]objectForKey:@"userPhone"];
    }
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"goBack" object:nil];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

#pragma mark - 在此处检测网络状态，如果有网就把未上传的历史数据上传
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if ([LRUserManager createManager].isLogIn&&[LRTools checkNetWork]) {
        NSMutableArray *updateArray = [[LRDatabaseManager createDatabase]getUpdateArray];
        
        for (UpdateModel *tempModel in updateArray) {
            
            dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_LOW ,0),^{
                NSDictionary *resposeDic;
                if ([tempModel.updateType isEqualToString:@"pm"]) {
                    resposeDic = [[LRHttpManager createHttpManager]updatePMByPhone:tempModel.updatePhone AndLongitude:tempModel.updateLongitude AndLatitude:tempModel.updateLatitude AndCelsius:tempModel.updateCelsius AndDampness:tempModel.updateDampness AndAddress:tempModel.updateAddress AndPlacediy:tempModel.updatePlacediy AndPm:tempModel.updatePM25 AndPm2:tempModel.updatePM50 AndPm10:tempModel.updatePM100];
                } else if ([tempModel.updateType isEqualToString:@"Co2"]) {
                    resposeDic = [[LRHttpManager createHttpManager]updateCO2ByPhone:tempModel.updatePhone AndLongitude:tempModel.updateLongitude AndLatitude:tempModel.updateLatitude AndCelsius:tempModel.updateCelsius AndDampness:tempModel.updateDampness AndAddress:tempModel.updateAddress AndPlacediy:tempModel.updatePlacediy AndCO2:tempModel.updateCO2 AndCheckType:@"0"];
                } else if ([tempModel.updateType isEqualToString:@"hcho"]) {
                    resposeDic = [[LRHttpManager createHttpManager]updateHCHOByPhone:tempModel.updatePhone AndLongitude:tempModel.updateLongitude AndLatitude:tempModel.updateLatitude AndCelsius:tempModel.updateCelsius AndDampness:tempModel.updateDampness AndAddress:tempModel.updateAddress AndPlacediy:tempModel.updatePlacediy AndHCHO:tempModel.updateHOCO];
                }
                if ([[resposeDic objectForKey:@"code"] integerValue]==0) {
                    [[LRDatabaseManager createDatabase]deleteUpdateModelByUpdateId:tempModel.updateID];
                    NSLog(@"上传成功");
                } else {
                    [[LRDatabaseManager createDatabase]setUpdateModelFailByUpdateId:tempModel.updateID];
                    NSLog(@"上传失败");
                }
            });
            
        }
    }
}


- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
